﻿namespace MyWebLog.Logic.Models.Security
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using DatabaseAbstraction.Interfaces;
    using MyWebLog.Logic.Models.Constants;
    using System.Data;
    using DatabaseAbstraction.Utils;

    /// <summary>
    /// This represents a blog user
    /// </summary>
    public class BlogUser : IParameterProvider
    {
        #region Properties

        /// <summary>
        /// The ID of this user
        /// </summary>
        public int ID { get; set; }

        /// <summary>
        /// The status of this user (<see cref="MyWebLog.Logic.Models.Constants.UserStatus"/>)
        /// </summary>
        public UserStatus Status { get; set; }

        /// <summary>
        /// The e-mail address for this user
        /// </summary>
        public string EmailAddress { get; set; }

        /// <summary>
        /// The password hash for this user
        /// </summary>
        /// <remarks>
        /// This is only populated when it is required (password setting)
        /// </remarks>
        public string PasswordHash { get; set; }

        /// <summary>
        /// The first name of the user
        /// </summary>
        public string FirstName { get; set; }

        /// <summary>
        /// The last name of the user
        /// </summary>
        public string LastName { get; set; }

        /// <summary>
        /// The display name of the user (the name shown publicly as author, commentor, etc.)
        /// </summary>
        public string DisplayName { get; set; }

        /// <summary>
        /// The date this user was created
        /// </summary>
        public DateTime CreatedDate { get; set; }

        /// <summary>
        /// The date this user last logged in
        /// </summary>
        public DateTime? LastSeenDate { get; set; }

        /// <summary>
        /// The verification key for pending users
        /// </summary>
        public string VerificationKey { get; set; }

        #endregion

        #region Constructors

        /// <summary>
        /// Constructor (empty)
        /// </summary>
        public BlogUser() { }

        /// <summary>
        /// Constructor (filled)
        /// </summary>
        /// <param name="reader">
        /// An open data reader, pointing to the row to use
        /// </param>
        public BlogUser(IDataReader reader)
        {
            ID = reader.GetInt32(reader.GetOrdinal("blog_user_id"));
            Status = (UserStatus)reader.GetInt32(reader.GetOrdinal("status_id"));
            EmailAddress = reader.GetString(reader.GetOrdinal("email_address"));
            FirstName = reader.GetString(reader.GetOrdinal("first_name"));
            LastName = reader.GetString(reader.GetOrdinal("last_name"));
            DisplayName = reader.GetString(reader.GetOrdinal("display_name"));
            CreatedDate = reader.GetDateTime(reader.GetOrdinal("created_date"));
            LastSeenDate = NullUtils.GetDateTimeOrNull(reader, "last_seen_date");
            VerificationKey = NullUtils.GetStringOrNull(reader, "verification_key");
        }

        #endregion

        #region Interface Implementation

        /// <summary>
        /// This object's properties as a dictionary, for use in persistence
        /// </summary>
        /// <returns>
        /// A key/value dictionary of the properties of this object
        /// </returns>
        public IDictionary<string, object> Parameters()
        {
            var parameters = new Dictionary<string, object>();

            parameters.Add("blog_user_id", ID);
            parameters.Add("status_id", (int)Status);
            parameters.Add("email_address", EmailAddress);
            parameters.Add("password_hash", PasswordHash);
            parameters.Add("first_name", FirstName);
            parameters.Add("last_name", LastName);
            parameters.Add("display_name", DisplayName);
            parameters.Add("created_date", CreatedDate);

            if (LastSeenDate.HasValue)
                parameters.Add("last_seen_date", LastSeenDate.Value);
            else
                parameters.Add("last_seen_date", null);

            parameters.Add("verification_key", (String.IsNullOrEmpty(VerificationKey)) ? null : VerificationKey);

            return parameters;
        }

        #endregion
    }
}